package com.yhm.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.yhm.dao.AllDAO;
import com.yhm.model.OrderFrom;
import com.yhm.model.Trace;
import com.yhm.service.OrderManager;
import com.yhm.util.page.OrderPage;


@Component("orderManager")
public class OrderManagerImpl implements OrderManager{
	
	private AllDAO allDAO = new AllDAO();
	private SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 添加订单
	 */
	public String order_add(String comNum,OrderFrom of){
		String json;
		OrderFrom orderfrom = (OrderFrom) allDAO.getOrderFromDAO().findById(comNum);
		if(orderfrom!=null){
			json = "{\"status\":1,\"mess\":\"该订单号已存在！\",";
			return json;
		}
		allDAO.getOrderFromDAO().save(of);
		json = "{\"status\":0,\"mess\":\"添加新订单成功\",";
		return json;
	}
	
	/**
	 * 获取订单信息
	 */
	public String get_order(String companyNum){
		String json = "";
		OrderFrom of = (OrderFrom) allDAO.getOrderFromDAO()
				.findById(companyNum);
		if(of.getCustomNum()==null) of.setCustomNum("");
		if(of.getCustomType()==null) of.setCustomType("");
		if(of.getMarks()==null) of.setMarks("");
		if(of.getMovement()==null) of.setMovement("");
		if(of.getNum()==null) of.setNum("");
		json = "{\"companyNum\":\""+of.getCompanyNum()+"\",\"customNum\":\""+of.getCustomNum()+"\",\"customType\":\""+of.getCustomType()+"\"," +
				"\"marks\":\""+of.getMarks()+"\",\"movement\":\""+of.getMovement()+"\",\"num\":\""+of.getNum()+"\"" +
				",";
		return json;
	}
	
	/**
	 * 修改订单
	 */
	public String order_update(String companyNum,String customNum,String customType,String movement,String marks,String num){
		String json = "";
		OrderFrom of = (OrderFrom) allDAO.getOrderFromDAO()
				.findById(companyNum);
		of.setCustomNum(customNum);
		of.setCustomType(customType);
		of.setMovement(movement);
		of.setMarks(marks);
		of.setNum(num);
		allDAO.getOrderFromDAO().attachDirty(of);
		json = "{\"status\":0,\"mess\":\"修改订单成功\"}";
		return json;
	}
	
	/**
	 * 删除订单
	 */
	public String order_del(String companyNum){
		String json = "";
			OrderFrom of = new OrderFrom();
			of.setCompanyNum(companyNum);
			allDAO.getOrderFromDAO().delete(of);
			allDAO.getTraceDAO()
					.delhql("from Trace t where t.companyNum = '"
							+ companyNum + "'");
			allDAO.getRedoDAO().delhql("from Redo t where t.companyNum = '"
					+ companyNum + "'");
		json = "{\"status\":0,\"mess\":\"删除订单成功\"}";
		return json;
	}
	
	/**
	 * 删除多个订单
	 */
	public String order_del_s(String[] ck){
		String json = "";
		for(int i=0;i<ck.length;i++){
			OrderFrom of = new OrderFrom();
			of.setCompanyNum(ck[i]);
			allDAO.getOrderFromDAO().delete(of);
			allDAO.getTraceDAO()
					.delhql("from Trace t where t.companyNum = '"
							+ ck[i] + "'");
			allDAO.getRedoDAO().delhql("from Redo t where t.companyNum = '"
					+ ck[i] + "'");
		}
		json = "{\"status\":0,\"mess\":\"删除订单成功！！\"}";
		return json;
	}
	
	/**
	 * 时间段删除
	 * @param begin_time
	 * @param end_time
	 * @return
	 */
	public String order_mouth_del(String begin_time,String end_time){
		String json = "";
		try {
			allDAO.getOrderFromDAO().delhql("from Trace t where t.companyNum in (select companyNum from OrderFrom o where o.companyNum = t.companyNum and (o.makeTime between '"+begin_time+"' and '"+end_time+"'))");
			allDAO.getOrderFromDAO().delhql("from Redo r where r.companyNum in (select companyNum from OrderFrom o where o.companyNum = r.companyNum and (o.makeTime between '"+begin_time+"' and '"+end_time+"'))");
			allDAO.getOrderFromDAO().delhql("from OrderFrom o where (o.makeTime between '"+begin_time+"' and '"+end_time+"')");
			json = "{\"status\":0,\"mess\":\"删除订单成功！！\"}";
		} catch (Exception e) {
			e.printStackTrace();
			json = "{\"status\":1,\"mess\":\"删除订单失败！！\"}";
		}
		return json;
	}
	
	/**
	 * 获取订单列表
	 */
	@SuppressWarnings("unchecked")
	public String get_order_list(OrderPage page) throws ParseException{
		String json = "";
		String string_order = "";
		if (page.getCompanyNum().length() != 3) {
			page.setCompanyNum("");
		}
		if (page.getSerial_number().length() != 4) {
			page.setSerial_number("");
		}
		if (page.getSelImportant().length() != 4) {
			page.setSelImportant("");
		}
		if (page.getCompanyNum().length()!=0&&page.getSerial_number().length()!=0&&page.getSelImportant().length()!=0) {
			string_order = "o.companyNum like '"+page.getCompanyNum()+"_____"+page.getSerial_number()+""+page.getSelImportant()+"'";
		}
		if (page.getCompanyNum().length()!=0&&page.getSerial_number().length()!=0&&page.getSelImportant().length()==0) {
			string_order = "o.companyNum like '"+page.getCompanyNum()+"_____"+page.getSerial_number()+"____'";
		}
		if (page.getCompanyNum().length()!=0&&page.getSerial_number().length()==0&&page.getSelImportant().length()!=0) {
			string_order = "o.companyNum like '"+page.getCompanyNum()+"_________"+page.getSelImportant()+"'";
		}
		if (page.getCompanyNum().length()==0&&page.getSerial_number().length()!=0&&page.getSelImportant().length()!=0) {
			string_order = "o.companyNum like '________"+page.getSerial_number()+""+page.getSelImportant()+"'";
		}
		if (page.getCompanyNum().length()!=0&&page.getSerial_number().length()==0&&page.getSelImportant().length()==0) {
			string_order = "o.companyNum like '"+page.getCompanyNum()+"_____________'";
		}
		if (page.getCompanyNum().length()==0&&page.getSerial_number().length()!=0&&page.getSelImportant().length()==0) {
			string_order = "o.companyNum like '________"+page.getSerial_number()+"____'";
		}
		if (page.getCompanyNum().length()==0&&page.getSerial_number().length()==0&&page.getSelImportant().length()!=0) {
			string_order = "o.companyNum like '____________"+page.getSelImportant()+"'";
		}
		
		if (page.getCustomNum().length() != 0) {
			if (string_order.length() == 0)
				string_order = string_order + " " + "o.customNum like" + " " + "'%"
						+ page.getCustomNum() + "%'";
			else {
				string_order = string_order + " " + "and" + " " + "o.customNum like" + " " + "'%"
						+ page.getCustomNum() + "%'";
			}
		}
		
		if (page.getCustomType().length() != 0) {
			if (string_order.length() == 0)
				string_order = string_order + " " + "o.customType like" + " " + "'%"
						+ page.getCustomType() + "%'";
			else {
				string_order = string_order + " " + "and" + " " + "o.customType like" + " " + "'%"
						+ page.getCustomType() + "%'";
			}
		}
		if (page.getMarks().length() != 0) {
			if (string_order.length() == 0)
				string_order = string_order + " " + "o.marks like" + " " + "'%" + page.getMarks() + "%'";
			else {
				string_order = string_order + " " + "and" + " " + "o.marks like" + " " + "'%"
						+ page.getMarks() + "%'";
			}
		}
		if (page.getMovement().length() != 0) {
			if (string_order.length() == 0)
				string_order = string_order + " " + "o.movement like" + " " + "'%" + page.getMovement()
						+ "%'";
			else {
				string_order = string_order + " " + "and" + " " + "o.movement like" + " " + "'%"
						+ page.getMovement() + "%'";
			}
		}
		if(page.getBegin_time().length()!=0&&page.getEnd_time().length()!=0){
			if (string_order.length() == 0)
				string_order = string_order + " " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+page.getEnd_time()+"')";
			else {
				string_order = string_order + " " + "and" + "(o.makeTime " + "between '"+page.getBegin_time()+"' and '"+page.getEnd_time()+"')";
			}
		}
		if(page.getBegin_time().length()==0&&page.getEnd_time().length()!=0){
			if (string_order.length() == 0)
				string_order = string_order + " " + "(o.makeTime between '2012-10-10' and '"+page.getEnd_time()+"')";
			else {
				string_order = string_order + " " + "and" + "(o.makeTime " + "between '2012-10-10' and '"+page.getEnd_time()+"')";
			}
		}
		if(page.getBegin_time().length()!=0&&page.getEnd_time().length()==0){
			Date date = new Date();
			String now = df.format(date);
			if (string_order.length() == 0)
				string_order = string_order + " " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+now+"')";
			else {
				string_order = string_order + " " + "and" + "(o.makeTime " + "between '"+page.getBegin_time()+"' and '"+now+"')";
			}
		}
		if(!page.getDep().equals("0")){
			if (string_order.length() == 0)
				string_order = string_order + " " + "o.department like" + " " + "'%" + page.getDep()
						+ "%'";
			else {
				string_order = string_order + " " + "and" + " " + "o.department like" + " " + "'%"
						+ page.getDep() + "%'";
			}
		}
		String hql = "";
		
		if(string_order.length()==0){
			if(page.getStatus().equals("0")){
				hql = "from OrderFrom o where o.qc2 = '"+page.getStatus()+"'";
			}else{
				hql = "from OrderFrom o where o.qc2 != '0'";
			}
		}else{
			if(page.getStatus().equals("0")){
				hql = "from OrderFrom o where o.qc2 = '"+page.getStatus()+"' and " + string_order +"";
			}else{
				hql = "from OrderFrom o where o.qc2 != '0' and " + string_order +"";
			}
		}
		//System.out.println(hql);
		List<?> order_lists = allDAO.getOrderFromDAO().findhql("select COUNT(*) " + hql);
		int order_lists_size = ((Long) order_lists.get(0)).intValue();
		//System.out.println(order_lists_size);
		if(order_lists_size == 0){
			json = "{\"total\":0,\"rows\":[]}";
			return json;
		}
		page.countPage(order_lists_size);
		List<OrderFrom> order_list = (List<OrderFrom>) allDAO.getOrderFromDAO().createQuery(hql+ "order by o.makeTime DESC"
				,page.getFirstNum(),page.getNum());
		json = "{\"total\":" + page.getCountNums() + "," +
				"\"rows\":[";
		
		for (OrderFrom of : order_list) {
			String thql = "from Trace t where t.companyNum = '"+of.getCompanyNum()+"' order by t.regtime DESC";
			List<Trace> ltrace = (List<Trace>) allDAO.getTraceDAO().findhql(thql);
			String dep = "";
			String time = "";
			String qc1 = "";
			String qc2 = "";
			if(ltrace==null||ltrace.size()==0){
				dep = "写字楼";
				Date begin = df.parse(of.getMakeTime());
				Date end = df.parse(df.format(new Date()));
				long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
				long day1 = between / (24 * 3600);
				time = "停留" + day1 + "天";
			}else{
				dep = ltrace.get(0).getDepartment();
				if (ltrace.get(0).getOuttime() == null){
					Date begin = dfs.parse(ltrace.get(0).getRegtime());
					Date end = dfs.parse(dfs.format(new Date()));
					time = "停留"+time_difference(begin,end);
				}else if(ltrace.get(0).getOuttime()!=null){
					time = "已离开";
				}
			}
			if(of.getQc1().equals("0")){
				qc1 = "未检查";
			}else{
				qc1 = of.getQc1()+"号检查";
			}
			if(of.getQc2().equals("0")){
				qc2 = "未出货";
			}else{
				qc2 = of.getQc2()+"号检查";
			}
			json = json.concat("{\"companyNum\":\"" + of.getCompanyNum() + "\"," +
					"\"ck\":\"" + of.getCompanyNum() + "\"," +
					"\"customNum\":\"" + of.getCustomNum() + "\"," +
					"\"customType\":\"" + of.getCustomType() + "\"," +
					"\"marks\":\"" + of.getMarks() + "\"," +
					"\"movement\":\"" + of.getMovement() + "\"," +
					"\"num\":\"" + of.getNum() + "\"," +
					"\"makeTime\":\"" + of.getMakeTime() + "\"," +
					"\"dep\":\"" + dep + "\"," +
					"\"time\":\"" + time + "\"," +
					"\"qc1\":\"" + qc1 + "\"," +
					"\"qc2\":\"" + qc2 + "\"" +
					"},");
		}
		if(order_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]}");
		else
			json = json.concat("]}");
		return json;
	}
	
	/**
	 * 获取追溯信息
	 */
	@SuppressWarnings("unchecked")
	public String get_order_trace(String companyNum){
		String json = "";
		String hql = "from Trace t where t.companyNum = '"+companyNum+"' order by t.regtime ASC";
		List<Trace> trace_list = (List<Trace>) allDAO.getTraceDAO().findhql(hql);
		json = "{\"total\":0 ," +
				"\"rows\":[";
		String com = "";
		for(Trace t :trace_list){
			if(t.getOuttime()!=null) com = t.getOuttime();
			json = json.concat("{\"dep\":\"" + t.getDepartment() + "\"," +
					"\"regtime\":\"" + t.getRegtime() + "\"," +
					"\"outtime\":\"" + com + "\"" +
					"},");
		}
		if(trace_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]}");
		else
			json = json.concat("]}");
		return json;
	}
	
	
	/**
	 * 时间差
	 * @param begin
	 * @param end
	 * @return
	 */
	public static String time_difference(Date begin,Date end){
		long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		return "" + day1 + "天" + hour1 + "小时";
	}
	

	@Resource(name = "allDAO")
	public void setAllDAO(AllDAO allDAO) {
		this.allDAO = allDAO;
	}
}
